import store from "../store";
import api from "../api";
import bus from './bus.js'
export default {
  /**
   * 判断是否是数字
   * @param {} val
   * @returns
   */
  isRealNum(val) {
    // isNaN()函数 把空串 空格 以及NUll 按照0来处理 所以先去除，
    if (val === "" || val == null) {
      return false;
    }
    if (!isNaN(val)) {
      //对于空数组和只有一个数值成员的数组或全是数字组成的字符串，isNaN返回false，例如：'123'、[]、[2]、['123'],isNaN返回false,
      //所以如果不需要val包含这些特殊情况，则这个判断改写为if(!isNaN(val) && typeof val === 'number' )
      return true;
    } else {
      return false;
    }
  },
  /**
   * 判断是否是空串
   * @param {字符串} str
   * @returns
   */
  isEmptyString(str) {
    if (str.replace(/(^\s*)|(\s*$)/g, "").length == 0) {
      return true;
    }
    return false;
  },
  /**
   *
   * @param {字符串} str
   * @returns 长度
   */
  getBLen(str) {
    if (str == null) return 0;
    if (typeof str != "string") {
      str += "";
    }
    return str.replace(/[^\x00-\xff]/g, "01").length;
  },
  getNickname(name) {
    return this.subLongString(name, 8);
  },
  subLongString(str, len) {
    if (this.getBLen(str) > len) {
      var count = 1;
      var res;
      while (true) {
        res = str.substr(0, count);
        if (this.getBLen(res) >= len) {
          return res + "...";
        }
        ++count;
      }
    }
    return str;
  },
  formatNumber(num) {
    if (num > 0) {
      return num >= 1e3 && num < 1e4 ?
        (num / 1e3).toFixed(1) + "k" :
        num >= 1e4 ?
        (num / 1e4).toFixed(1) + "w" :
        num;
    } else {
      num = -num;
      return num >= 1e3 && num < 1e4 ?
        '-' + (num / 1e3).toFixed(1) + "k" :
        num >= 1e4 ?
        '-' + (num / 1e4).toFixed(1) + "w" :
        num;
    }

  },
  hasToken() {
    return store.getters.getToken.length != 0;
  },
  map(dec) {
    switch (dec) {
      case 'DOCUMENT':
        return '文章';
        case 'MESSAGE':
        return '消息';
      case 'USER':
        return '博主';
      case 'DELETE':
        return '删除';
      case 'NORMAL':
        return '正常';
      case 'MAN':
        return '男';
      case 'WOMAN':
        return '女';
      case 'DRAFT':
        return '草稿';
      case 'RELEASE':
        return '发布';
      case 'TOP':
        return '置顶';
      case 'SELF':
        return '自己';
      case 'UNREAD':
        return '未读';
      case 'READ':
        return '已读';
      case 'SHARE':
        return '分享';
      case 'NOT_SHARE':
        return '取消分享';
      case 'CONCERNED':
        return '已关注';
      case 'BE_CONCERNED':
        return '被关注';
      case 'CLOSELY':
        return '互相关注';
      case 'BLOCK':
        return '黑名单';
      case 'MAIL':
        return '邮件未验证';
      case 'ADMIN':
        return '管理员';
      case 'ROOT':
        return '超级用户';
      case 'TITLED':
        return '封号';
    }
  },
  role() {
    return store.getters.getRole;
  },
  colorRGB2Hex(r, g, b) {
    let hex = "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
    return hex;
  },
  // 获得补充色
  getColorIsDeep(url) {
    const img = new Image();
    img.src =
      url;
    img.setAttribute("crossOrigin", "anonymous");
    img.onload = function () {
      let palette = new Palette(img);
      const mainColor = palette.getVibrantColor();

      let rgb = mainColor.split(",");
      let r, g, b;
      r = rgb[0].replace(/[^0-9]/gi, "");
      g = rgb[1].replace(/[^0-9]/gi, "");
      b = rgb[2].replace(/[^0-9]/gi, "");
      let colorLeave = r * 0.299 + g * 0.587 + b * 0.114
      return colorLeave < 192

    };
  },
  isMobile() {
    let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
    return flag;
  },
  minWidth(num) {
    var windowWidth = $(window).width();
    if (windowWidth < num) {
      return false
    }
    return true;
  },
  timestampToTime(timestamp) {
    var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = date.getMinutes() + ':';
    var s = date.getSeconds();
    return Y+M+D+h+m+s;
},
  myDate(value, type = 0) {
    if (value < 0) return '永久';
    var time = new Date(value);
    var year = time.getFullYear();
    var month = time.getMonth() + 1;
    var date = time.getDate();
    var hour = time.getHours();
    var minute = time.getMinutes();
    var second = time.getSeconds();
    month = month < 10 ? "0" + month : month;
    date = date < 10 ? "0" + date : date;
    hour = hour < 10 ? "0" + hour : hour;
    minute = minute < 10 ? "0" + minute : minute;
    second = second < 10 ? "0" + second : second;
    var arr = [
      year + "-" + month + "-" + date,
      year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second,
      year + "年" + month + "月" + date,
      year + "年" + month + "月" + date + " " + hour + ":" + minute + ":" + second,
      hour + ":" + minute + ":" + second
    ]
    return arr[type];
  },
  getCurrentDate(format) {
    var now = new Date();
    var year = now.getFullYear(); //得到年份
    var month = now.getMonth(); //得到月份
    var date = now.getDate(); //得到日期
    var day = now.getDay(); //得到周几
    var hour = now.getHours(); //得到小时
    var minu = now.getMinutes(); //得到分钟
    var sec = now.getSeconds(); //得到秒
    month = month + 1;
    if (month < 10) month = "0" + month;
    if (date < 10) date = "0" + date;
    if (hour < 10) hour = "0" + hour;
    if (minu < 10) minu = "0" + minu;
    if (sec < 10) sec = "0" + sec;
    var time = "";
    //精确到天
    if (format == 1) {
      time = year + "-" + month + "-" + date;
    }
    //精确到分
    else if (format == 2) {
      time = year + "-" + month + "-" + date + " " + hour + ":" + minu + ":" + sec;
    }
    return time;
  },
  overflowHidForText(val, max = 42) {
    let textLength = val.length;
    let resultText = '';
    if (textLength > max) {
      resultText = val.slice(0, max) + '...'
    } else {
      resultText = val
    }
    return resultText
  },
  dateStr(tmpTime) {
    var mm = 1000; //1000毫秒 代表1秒
    var minute = mm * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var month = day * 30;
    var ansTimeDifference = 0; //记录时间差
    var tmpTimeStamp = tmpTime
    var nowTime = new Date().getTime(); //获取当前时间戳
    var tmpTimeDifference = nowTime - tmpTimeStamp; //计算当前与需要计算的时间的时间戳的差值
    if (tmpTimeDifference < 0) { //时间超出，不能计算
      alert("开始日期大于结束日期，计算失败！");
      return 0;
    }
    /**
     * 通过最开始强调的各个时间段用毫秒表示的数值，进行时间上的取整，为0的话，则没有到达
     * */
    var DifferebceMonth = tmpTimeDifference / month; //进行月份取整
    var DifferebceWeek = tmpTimeDifference / (7 * day); //进行周取整
    var DifferebceDay = tmpTimeDifference / day; //进行天取整
    var DifferebceHour = tmpTimeDifference / hour; //进行小时取整
    var DifferebceMinute = tmpTimeDifference / minute; //进行分钟取整
    if (DifferebceMonth >= 1) {
      return this.myDate(tmpTime); //大于一个月 直接返回时间
    } else if (DifferebceWeek >= 1) {
      ansTimeDifference = parseInt(DifferebceWeek) + "个星期前";
    } else if (DifferebceDay >= 1) {
      ansTimeDifference = parseInt(DifferebceDay) + "天前";
    } else if (DifferebceHour >= 1) {
      ansTimeDifference = parseInt(DifferebceHour) + "个小时前";
    } else if (DifferebceMinute >= 1) {
      ansTimeDifference = parseInt(DifferebceMinute) + "分钟前";
    } else {
      ansTimeDifference = "刚刚";
    }
    return ansTimeDifference;

  },
  toWindodAuthor(id) {
    window.open("#/author/" + id);
  },
  toWindodBlog(id) {
    window.open("#/blog/" + id);
  },
  toWindodMessage(id) {
    window.open("#/message/" + id);
  },
  toSearch(data) {
    // 快速搜索
    const pram = {
      name: 'open',
      data: data
    }
    bus.$emit('openSearch', pram);
  },
  validateEmail(emailAddr) {
    var pattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var res = pattern.test(emailAddr)
    return res;
  }

};
